package main

import (
	"fmt"
	"net/http"
)

func myHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Println(r.RemoteAddr, "连接成功")

	fmt.Println("method:", r.Method) //获取请求的方法

	fmt.Println("url:", r.URL) //获取url

	fmt.Println("header:", r.Header) //获取header

	fmt.Println("body:", r.Body) //获取body

	w.Write([]byte("hello world")) //写入response body
}

func login(w http.ResponseWriter, r *http.Request) {
	username := r.URL.Query().Get("username")
	password := r.URL.Query().Get("password")
	if username == "admin" && password == "123456" {
		w.Write([]byte("登录成功"))
	}
	w.Write([]byte("登录失败"))
}

func main() {
	fmt.Println("http server start")
	http.HandleFunc("/go", myHandler)
	http.HandleFunc("/login", login)
	// users
	http.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("users"))
	})
	// products
	http.HandleFunc("/products", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("products"))
	})
	// orders
	http.HandleFunc("/orders", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("orders"))
	})

	// addr: 监听的地址
	// handler: 回调函数
	http.ListenAndServe(":80", nil)
}
